package com.leapp.sk.util

import android.app.Activity
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.support.v4.content.FileProvider
import com.example.mvp.view.imgselector.ImageSelector
import com.leapp.sk.inter.ImageHeadPathInterface
import com.example.util.finallist.PathFinalUtil
import com.example.util.finallist.USERINFO_CLIP_REQUEST
import java.io.File

/**
 * 图片裁剪
 * @author  chaohaipeng  on 2018/7/5.
 */
class HeadImageCropUtil {
    companion object {
        var headFile: File? = null
        /**
         * 头像裁剪
         */
        private fun startPhotoZoom(uri: Uri?, requestCode: Int, context: Activity): File {
            val intent = Intent("com.android.camera.action.CROP")
            intent.setDataAndType(uri, "image/*")
            intent.putExtra("crop", "true")
            intent.putExtra("aspectX", 1)
            intent.putExtra("aspectY", 1)
            intent.putExtra("outputX", 800)
            intent.putExtra("outputY", 800)
            intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString())
            val f1 = File(PathFinalUtil.HEAD_PATH)
            if (!f1.exists()) {
                f1.mkdirs()
            }
            var mFileUri: File
            mFileUri = File(f1, PathFinalUtil.HEAD_PATH_NAME)
            if (mFileUri.exists()) {
                mFileUri.delete()
            }
            if (Build.VERSION.SDK_INT >= 24) {
                val uriForFile:Uri = FileProvider.getUriForFile(context, "com.example.baselibrary.fileprovider", mFileUri);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile)
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
            } else {
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFileUri))
            }
            intent.putExtra("return-data", false) //裁剪后的数据不以bitmap的形式返回
            context.startActivityForResult(intent, requestCode)
            return mFileUri
        }
        /**
         * Activity结果返回回调
         */
        fun getResultHeadFile(requestCode: Int, resultCode: Int, data: Intent?, context: Activity, headImageInterface: ImageHeadPathInterface) {
            if (requestCode == ImageSelector.REQUEST_SELECT_IMAGE) {
                if (resultCode == RESULT_OK) {
                    val imagesPath = data?.getStringArrayListExtra(ImageSelector.SELECTED_RESULT)
                    if (imagesPath != null && imagesPath?.size > 0) {
                        val file = File(imagesPath?.get(0))
                        if (file.exists()) {
                            val uri = Uri.fromFile(file)
                            val _uri = ParseFilePathUtil.getUri(context, uri)
                            headFile = null
                            headFile = startPhotoZoom(_uri, USERINFO_CLIP_REQUEST, context)
                        }
                    }
                }
            } else if (requestCode == USERINFO_CLIP_REQUEST) {
                    if (headFile?.exists()!!) {
                        headImageInterface.getImageHeadFile(headFile)
                    }
            }
        }
    }
}